import { ResPage } from "@/api/interface";
import { CircleFriend } from "@/api/interface/circleFriend";
import {
	addCircleFriend,
	cancelTopCircleFriend,
	deleteCircleFriend,
	getCircleFriendDetail,
	getCircleFriendList,
	topCircleFriend,
	updateCircleFriendApi
} from "@/api/modules/circleFriend";
import { uploadFile } from "@/api/modules/common";
import { RootThunkAction, store } from "@/redux";
import { Action } from "@/redux/interface";
import * as types from "@/redux/mutation-types";
import { Dispatch } from "redux";

export const saveCircleFriendList = (circleFriendList: ResPage<CircleFriend>) => {
	return {
		type: types.SET_CIRCLEFRIEND_LIST,
		payload: circleFriendList
	};
};

export const saveCircleFriendDetail = (circleFriend: CircleFriend) => {
	return {
		type: types.SET_CIRCLEFRIEND_DETAIL,
		payload: circleFriend
	};
};

export const fileUpload = async (formData: FormData) => {
	return await store.dispatch(upload(formData));
};

export const getCircleFriendLists = (page: number = 1, size: number = 10, wrapper?: Partial<CircleFriend>) => {
	store.dispatch(getList(page, size, wrapper));
};

export const removeCircleFriend = (id: number) => {
	store.dispatch(remove(id));
};

export const postCircleFriend = (circleFriend: CircleFriend) => {
	store.dispatch(add(circleFriend));
};

export const getCircleFriendDetails = (id: number) => {
	store.dispatch(getDetail(id));
};

export const updateCircleFriend = (circleFriend: Partial<CircleFriend>) => {
	store.dispatch(update(circleFriend));
};

export const topCircleFriends = async (id: number) => {
	return await store.dispatch(top(id));
};

export const cancelTopCircleFriends = async (id: number) => {
	return await store.dispatch(cancelTop(id));
};

export const getList = (page: number, size: number, wrapper?: Partial<CircleFriend>): RootThunkAction => {
	return async (dispatch: Dispatch<Action<ResPage<CircleFriend>>>) => {
		const res = await getCircleFriendList(page, size, wrapper);
		dispatch(saveCircleFriendList(res.data) ?? {});
	};
};

export const getDetail = (id: number): RootThunkAction => {
	return async (dispatch: Dispatch<Action<CircleFriend>>) => {
		const res = await getCircleFriendDetail(id);
		dispatch(saveCircleFriendDetail(res.data));
	};
};

export const remove = (id: number): RootThunkAction => {
	return async dispatch => {
		const res = await deleteCircleFriend(id);
		if (res.code === 200) dispatch(getList(1, 10));
	};
};

export const add = (circleFriend: CircleFriend): RootThunkAction => {
	return async dispatch => {
		const res = await addCircleFriend(circleFriend);
		// if (res.code === 200) dispatch(getList());
	};
};

export const update = (circleFriend: Partial<CircleFriend>): RootThunkAction => {
	return async dispatch => {
		const res = await updateCircleFriendApi(circleFriend);
		if (res.code === 200) dispatch(getList(1, 10));
	};
};

export const upload = (formData: FormData): RootThunkAction => {
	return async () => {
		const res = await uploadFile(formData);
		return res.data;
	};
};

export const top = (id: number): RootThunkAction => {
	return async () => {
		const res = await topCircleFriend(id);
		if (res.code === 200) return true;
		else return false;
	};
};

export const cancelTop = (id: number): RootThunkAction => {
	return async () => {
		const res = await cancelTopCircleFriend(id);
		if (res.code === 200) return true;
		else return false;
	};
};
